package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.QuestionUserLock;
import com.itheima.mapper.QuestionUserLockMapper;
import com.itheima.service.db.QuestionUserLockService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class QuestionUserLockServiceImpl implements QuestionUserLockService {

    @Autowired
    private QuestionUserLockMapper questionUserLockMapper;

    //查找是否锁定 通过用户id和问卷id查找
    @Override
    public QuestionUserLock findByUserIdAndQuestionnaireId(Long userId, Long id) {
        QueryWrapper<QuestionUserLock> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("questionnaire_id", id);
        QuestionUserLock questionUserLock = questionUserLockMapper.selectOne(wrapper);
        return questionUserLock;
    }

    //在tb_question_user_lock保存问卷解锁信息
    @Override
    public void save(QuestionUserLock questionUserLock) {
        QueryWrapper<QuestionUserLock> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", questionUserLock.getUserId());
        wrapper.eq("questionnaire_id", questionUserLock.getQuestionnaireId());
        QuestionUserLock questionUserLock1 = questionUserLockMapper.selectOne(wrapper);
        if (questionUserLock1 == null) {
            questionUserLockMapper.insert(questionUserLock);
        }
    }
}
